﻿using System;
using Microsoft.Xna.Framework;
using AngryBullets.Input;
using Microsoft.Xna.Framework.Input;

namespace AngryBullets.Sprite.Object
{
    class Baliste : AnimationSprite
    {
        #region Fields

        static private Vector2 _balistePosition;
        static private Vector2 _balisteCenter;
        private float _balisteSpeed = 1.0f;
        private int time;
        private KeyboardState keyboard;
        private MouseState mouse;

        #endregion

        #region Properties

        static public Vector2 BalistePosition
        {
            get { return _balistePosition; }
        }

        static public Vector2 BalisteCenter
        {
            get { return _balisteCenter; }
        }

        #endregion

        #region Initialisation

        public Baliste(Vector2 position)
            : base(new AnimationDefinition()
            {
                AssetName = "catapultWait",
                FrameRate = 1,
                FrameSize = new Point(130, 130),
                Loop = true,
                NbFrames = new Point(1, 1)
            })
        {
            _balistePosition = position;
        }

        #endregion

        #region Update

        public void Move(GameTime gameTime)
        {
            time = gameTime.ElapsedGameTime.Milliseconds;
            keyboard = InputState.CurrentKeyboardState;
            mouse = InputState.CurrentMouseState;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                base.Definition = new AnimationDefinition()
            {
                AssetName = "catapultShoot",
                FrameRate = 10,
                FrameSize = new Point(130, 130),
                Loop = true,
                NbFrames = new Point(10, 1)
            };
            }
            else if (keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right)
                && keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
            { }//Ne rien faire
            else if (keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
            {
                _balistePosition.X += _balisteSpeed * time / 8;
                base.Definition = new AnimationDefinition()
                {
                    AssetName = "catapultMove",
                    FrameRate = 8,
                    FrameSize = new Point(130, 130),
                    Loop = true,
                    NbFrames = new Point(8, 1)
                };
            }
            else if (keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
            {
                _balistePosition.X -= _balisteSpeed * time / 16;
                base.Definition = new AnimationDefinition()
                {
                    AssetName = "catapultMove",
                    FrameRate = 8,
                    FrameSize = new Point(130, 130),
                    Loop = true,
                    NbFrames = new Point(8, 1)
                };
            }
            else
            {
                base.Definition = new AnimationDefinition()
                {
                    AssetName = "catapultWait",
                    FrameRate = 1,
                    FrameSize = new Point(130, 130),
                    Loop = true,
                    NbFrames = new Point(1, 1)
                };
            }
            CalcCenter();
            base.Position.X = Convert.ToInt16(_balistePosition.X);
            base.Position.Y = Convert.ToInt16(_balistePosition.Y);
        }

        #endregion

        #region Methods

        private void CalcCenter()
        {
            _balisteCenter = _balistePosition + new Vector2(130 / 2, 130 / 2);
        }

        #endregion
    }
}
